package com.secneo.appshare.activities;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.adapter.ActivityUserAdapter;
import com.secneo.appshare.adapter.FeedsAdapter;
import com.secneo.appshare.adapter.OtherUserAppAdapter;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.Feed;
import com.secneo.appshare.api.RemoteApi.Feeds;
import com.secneo.appshare.api.RemoteApi.OtherUserApp;
import com.secneo.appshare.api.RemoteApi.OtherUserApps;
import com.secneo.appshare.api.RemoteApi.UserActivity;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.Util;
import com.secneo.appshare.threads.ActivityUserThread;
import com.secneo.appshare.threads.FeedsThread;
import com.secneo.appshare.threads.OtherUserAppThread;
import com.secneo.mp.api.database.DatabaseHelper;

public class AppsOtherUserActivity extends CommonMenuActivity
{
	enum UserAppPage{
		UserAppPage ,
		UserFlowPage ,
		UserFansPage ,
		UserActionPage
	}
	private String uid;
	private String other_uid;
	private String user_icon;
	private String user_name;
	private TextView topbarTxt;
	private LinearLayout userInfoLinear;
	private GridView mAppGridView;
	private TextView label_user_name;
	private TextView phone_type;
	private ImageView img_user_icon;
	private ImageView weibo_Icon;
	private ProgressBar progress_user_apps;
	private Button buttonApp;
	private Button buttonFollowed;
	private Button buttonFollower;
	private Button buttonAction;
	
	LinearLayout mBottomButtonLayout;


	private TextView numFollowedText;
	private TextView numFollowerText;
	private TextView numInstallText;
	private TextView numShareText;

	private TextView textFollowed; 
	private TextView textFollower;
	private TextView textInstall; 
	private TextView textShare;
	
	private String mPhoneTypeString;
	
	private Button followIt;
	private Button unfollowIt;
	private UserAppPage currentControl;
	private OtherUserAppThread userAppThread = null;
	private ActivityUserThread mActivityThread = null;
	private FeedsThread userFollowedThread = null;
	private FeedsThread userFollowerThread = null;
	private ListView userActivityListView;
	public static final int MSG_GET_OTHER_USER_ICON = 101;
	private OtherUserAppAdapter userAppAdapter = null;
	private FeedsAdapter feedAdapter = null;
	private ActivityUserAdapter userActivityAdapter = null;
	
	private Button btnPrivateMessage;
	
	private TextView isEmptyPromptTV = null ;

	private Handler mHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case Constants.MSG_GET_FEEDS_NETWORK_ERROR:
					View view = (View) findViewById(R.id.error_user);
					view.setVisibility(View.VISIBLE);
					progress_user_apps.setVisibility(View.INVISIBLE);
					break;
					
				case Constants.MSG_GET_FOLLOWED_FINISH:
					progress_user_apps.setVisibility(View.GONE);
					refreshGridView(userFollowedThread.getFeeds());
					break;
					
				case Constants.MSG_GET_FANS_FINISH:
					progress_user_apps.setVisibility(View.GONE);
					refreshGridView(userFollowerThread.getFeeds());
					break;
					
				case Constants.MSG_GET_FEEDICON_FINISH:
					mAppGridView.invalidateViews();
					break;
				
				case Constants.MSG_GET_OTHER_USER_ERROR:
					progress_user_apps.setVisibility(View.INVISIBLE);
					View view2 = (View) findViewById(R.id.error_user);
					view2.setVisibility(View.VISIBLE);
					break;
					
				case Constants.MSG_GET_OTHER_USER_STATUS_ERROR:
					progress_user_apps.setVisibility(View.INVISIBLE);
					break;
					
				case Constants.MSG_GET_USER_ACTIVITY_FINISH:
					progress_user_apps.setVisibility(View.INVISIBLE);
					refreshRecentActivity(mActivityThread.getActivitys());
					break;
					
				case Constants.MSG_GET_USER_ACTIVITYICON_FINISH:
					userActivityListView.invalidateViews();
					break;
					
				case Constants.MSG_GET_OTHER_USER_STATUS_OK:
					progress_user_apps.setVisibility(View.INVISIBLE);
					OtherUserApps otherUserApp = userAppThread.getOtherUserApps();
					refreshOtherUsersAppGridView(otherUserApp);
					break;
					
				case Constants.MSG_GET_OTHER_USER_APP_ICON_FINISH:
					mAppGridView.invalidateViews();
					break;
					
				case Constants.MSG_ACTION_FOLLOW_OK:
					Toast.makeText(AppsOtherUserActivity.this, R.string.follow_ok, 2000).show();
					followIt.setVisibility(View.GONE);
					unfollowIt.setVisibility(View.VISIBLE);
					break;
				case Constants.MSG_ACTION_FOLLOW_ERROR:
					Toast.makeText(AppsOtherUserActivity.this, R.string.follow_error, 2000).show();
					break;
				case Constants.MSG_ACTION_UNFOLLOW_OK:
					Toast.makeText(AppsOtherUserActivity.this, R.string.unfollow_ok, 2000).show();
					unfollowIt.setVisibility(View.GONE);
					followIt.setVisibility(View.VISIBLE);
					break;
				case Constants.MSG_ACTION_UNFOLLOW_ERROR:
					Toast.makeText(AppsOtherUserActivity.this, R.string.unfollow_error, 2000).show();
					break;

			}
			super.handleMessage(msg);
		}

	};

	private void startGetUserAppThread()
	{
		if (userAppThread != null)
		{
			userAppThread.stopThread();
			userAppThread = null;
		}
		userAppThread = new OtherUserAppThread(this, mHandler, uid, other_uid);
		userAppThread.start();
	}

	public void refreshOtherUsersAppGridView(OtherUserApps apps)
	{
		if(apps.otherUserAppList != null && apps.otherUserAppList.size() == 0){
			isEmptyPromptTV.setText(R.string.txt_other_no_apps) ;
			isEmptyPromptTV.setVisibility(View.VISIBLE) ;			
		}
		numInstallText.setText(String.valueOf(apps.numInstall));
		numShareText.setText(String.valueOf(apps.numShare));
		numFollowedText.setText(String.valueOf(apps.numFollowed));
		numFollowerText.setText(String.valueOf(apps.numFollower));

		TextView privateMsgNumber = (TextView) findViewById(R.id.private_msg_number);
		privateMsgNumber.setVisibility(View.GONE);

        String phoneTypeString = "(<u>"+apps.phoneType+"</u>)";
        mPhoneTypeString = apps.phoneType;
		phone_type.setText(Html.fromHtml(phoneTypeString)); 
		phone_type.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				DatabaseHelper dh=new DatabaseHelper(AppsOtherUserActivity.this);
				dh.insertUserAppPopularityBycatagory(AppsOtherUserActivity.this,2045);
				dh.close();
				
				Intent intent = new Intent() ;
				intent.setClass(AppsOtherUserActivity.this, PhoneUsersActivity.class) ;
				Bundle mBundle = new Bundle();
				mBundle.putString("phonename", mPhoneTypeString);
				intent.putExtras(mBundle);
				startActivity(intent) ;
				return;
			}
		});

		if(apps.snsType != 0)
		{
			int iconResource = LocalStore.getLocaleStoreInstance().getSmallIconResource(apps.snsType);
			if(iconResource!=0){
				weibo_Icon.setImageResource(iconResource);
				weibo_Icon.setVisibility(View.VISIBLE);
			}
		}
		else
		{
			weibo_Icon.setVisibility(View.INVISIBLE);
		}
		
		followIt.setVisibility(View.GONE);
		unfollowIt.setVisibility(View.GONE);
		//btnPrivateMessage.setVisibility(View.GONE);
		findViewById(R.id.paixu).setVisibility(View.GONE);
		if(apps.relation == Constants.FOLLOW_NONE)
		{
			followIt.setVisibility(View.VISIBLE);
		}
		else if(apps.relation == Constants.FOLLOW_ME)
		{
			followIt.setVisibility(View.VISIBLE);
			btnPrivateMessage.setVisibility(View.VISIBLE);
		}
		else if(apps.relation == Constants.FOLLOW_HIM)
		{
			unfollowIt.setVisibility(View.VISIBLE);
		}
		else if(apps.relation == Constants.FOLLOW_EACH)
		{
			unfollowIt.setVisibility(View.VISIBLE);
			btnPrivateMessage.setVisibility(View.VISIBLE);
		}
		
		if(uid.equals(other_uid))
		{
			followIt.setVisibility(View.GONE);
			btnPrivateMessage.setVisibility(View.GONE);
		}
		
		List<OtherUserApp> list = apps.otherUserAppList;
		mAppGridView.setVisibility(View.VISIBLE);
		mAppGridView.setNumColumns(4);
		userAppAdapter = new OtherUserAppAdapter(this, list);
		mAppGridView.setAdapter(userAppAdapter);

		ApplicationGridViewListener listener = new ApplicationGridViewListener(list);
		mAppGridView.setOnItemClickListener(listener);
	}

	@Override
	protected void onResume()
	{
		if(currentControl == UserAppPage.UserActionPage)
			userActivityListView.invalidateViews();
		else
			mAppGridView.invalidateViews();
		super.onResume();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.appshare_app_other_user);
		AppShareApplication.getInstance().addActivity(this);

		mBottomButtonLayout = (LinearLayout) findViewById(R.id.bottom_button);
		
		mAppGridView = (GridView) findViewById(R.id.grid_apps);
		label_user_name = (TextView) findViewById(R.id.label_user_name);
		phone_type = (TextView) findViewById(R.id.phone_type);
		img_user_icon = (ImageView) findViewById(R.id.img_user_icon);
		weibo_Icon = (ImageView) findViewById(R.id.weibo_Icon);
		progress_user_apps = (ProgressBar) findViewById(R.id.progress_user_apps);
		userInfoLinear = (LinearLayout) findViewById(R.id.panel_top);
		userActivityListView = (ListView) findViewById(R.id.private_msg_list);
		userActivityListView.setDivider(getResources().getDrawable(R.drawable.list_divider));
		userActivityListView.setVisibility(View.INVISIBLE);
		
		isEmptyPromptTV = (TextView)findViewById(R.id.empty_list_prompt) ;
		
		Button retButton = (Button)findViewById(R.id.img_ret) ;
		retButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if(UserAppPage.UserActionPage==currentControl){
					mBottomButtonLayout.setVisibility(View.VISIBLE);
					startGetUserApp();
					return;
				}
				else{
					finish();
				}
			}
		}) ;

		btnPrivateMessage = (Button) findViewById(R.id.btn_private_message);
		String text = AppsOtherUserActivity.this.getResources().getString(R.string.send_sms);
		btnPrivateMessage.setText(text);
		btnPrivateMessage.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				try
				{
					PopAlarmActivity.show(AppsOtherUserActivity.this, other_uid, user_name, 1, "");
				}
				catch (Exception e) {
					
					Toast.makeText(AppsOtherUserActivity.this, R.string.msg_error, 2000).show();
					e.printStackTrace();
				}
				return;
			}
		}) ;
		
		buttonApp = (Button) findViewById(R.id.button_app);
		buttonApp.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent() ;
				intent.setClass(AppsOtherUserActivity.this, OtherZoneAttentActivity.class) ;
				Bundle mBundle = new Bundle();
				mBundle.putString("userid", other_uid);
				mBundle.putString("username", user_name);
				mBundle.putString("avatarname", user_icon);
				intent.putExtras(mBundle);
				startActivity(intent) ;
				return;
			}
		});
		buttonFollowed = (Button) findViewById(R.id.button_followed);
		buttonFollowed.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent() ;
				intent.setClass(AppsOtherUserActivity.this, OtherZoneFansActivity.class) ;
				Bundle mBundle = new Bundle();
				mBundle.putString("userid", other_uid);
				mBundle.putString("username", user_name);
				mBundle.putString("avatarname", user_icon);
				intent.putExtras(mBundle);
				startActivity(intent) ;
				return;
			}
		});
		buttonFollower = (Button) findViewById(R.id.button_follower);
		buttonFollower.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.setClass(AppsOtherUserActivity.this, MyZoneHonorActivity.class);
				Bundle mBundle = new Bundle();
				mBundle.putString("userid", other_uid);
				mBundle.putString("username", user_name);
				mBundle.putString("avatarname", user_icon);
				intent.putExtras(mBundle);
				startActivity(intent);
				return;
			}
		});
		buttonAction = (Button) findViewById(R.id.button_action);
		buttonAction.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				DatabaseHelper dh=new DatabaseHelper(AppsOtherUserActivity.this);
				dh.insertUserAppPopularityBycatagory(AppsOtherUserActivity.this,2028);
				dh.close();
				startGetActivityThread();
				return;
			}
		});
		
		

		numInstallText = (TextView) findViewById(R.id.num_install);
		numInstallText.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				currentControl = UserAppPage.UserAppPage;
		//		startGetUserAppThread();
			}
		}) ;
		numShareText = (TextView) findViewById(R.id.num_share);
		numShareText.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				DatabaseHelper dh=new DatabaseHelper(AppsOtherUserActivity.this);
				dh.insertUserAppPopularityBycatagory(AppsOtherUserActivity.this,2028);
				dh.close();
				startGetActivityThread();
				return;
			}
		}) ;
		numFollowedText = (TextView) findViewById(R.id.num_followed);
		numFollowedText.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent() ;
				intent.setClass(AppsOtherUserActivity.this, OtherZoneAttentActivity.class) ;
				Bundle mBundle = new Bundle();
				mBundle.putString("userid", other_uid);
				mBundle.putString("username", user_name);
				mBundle.putString("avatarname", user_icon);
				intent.putExtras(mBundle);
				startActivity(intent) ;
				return;
			}
		});
		numFollowerText = (TextView) findViewById(R.id.num_follower);
		numFollowerText.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent() ;
				intent.setClass(AppsOtherUserActivity.this, OtherZoneFansActivity.class) ;
				Bundle mBundle = new Bundle();
				mBundle.putString("userid", other_uid);
				mBundle.putString("username", user_name);
				mBundle.putString("avatarname", user_icon);
				intent.putExtras(mBundle);
				startActivity(intent) ;
				return;
			}
		}) ;

		
		textInstall = (TextView) findViewById(R.id.word_install);
		textInstall.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				currentControl = UserAppPage.UserAppPage;
		//		startGetUserAppThread();
			}
		}) ;
		textShare = (TextView) findViewById(R.id.word_share);
		textShare.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				DatabaseHelper dh=new DatabaseHelper(AppsOtherUserActivity.this);
				dh.insertUserAppPopularityBycatagory(AppsOtherUserActivity.this,2028);
				dh.close();
				startGetActivityThread();
				return;
			}
		}) ;
		textFollowed = (TextView) findViewById(R.id.text_followed);
		textFollowed.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent() ;
				intent.setClass(AppsOtherUserActivity.this, OtherZoneAttentActivity.class) ;
				Bundle mBundle = new Bundle();
				mBundle.putString("userid", other_uid);
				mBundle.putString("username", user_name);
				mBundle.putString("avatarname", user_icon);
				intent.putExtras(mBundle);
				startActivity(intent) ;
				return;
			}
		}) ;
		textFollower = (TextView) findViewById(R.id.text_follower);
		textFollower.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent() ;
				intent.setClass(AppsOtherUserActivity.this, OtherZoneFansActivity.class) ;
				Bundle mBundle = new Bundle();
				mBundle.putString("userid", other_uid);
				mBundle.putString("username", user_name);
				mBundle.putString("avatarname", user_icon);
				intent.putExtras(mBundle);
				startActivity(intent) ;
				return;
			}
		}) ;
		
		
		followIt = (Button) findViewById(R.id.follow_it);
		followIt.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				try
				{
					Thread followThread = new Thread() 
					{
						public void run()
						{
							RemoteApiImpl remoteApi = new RemoteApiImpl();
							if(remoteApi.followSomeone(AppsOtherUserActivity.this, other_uid, true))
							{
								Message msg = new Message();
								msg.what = Constants.MSG_ACTION_FOLLOW_OK;
								mHandler.sendMessage(msg);
							}
							else
							{
								Message msg = new Message();
								msg.what = Constants.MSG_ACTION_FOLLOW_ERROR;
								mHandler.sendMessage(msg);
							}
						}
					};
					followThread.start();
				}
				catch (Exception e) {
					
					Toast.makeText(AppsOtherUserActivity.this, R.string.follow_error, 2000).show();
					e.printStackTrace();
				}
			}
		});
		unfollowIt = (Button) findViewById(R.id.unfollow_it);
		unfollowIt.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				try
				{
					Thread followThread = new Thread() 
					{
						public void run()
						{
							RemoteApiImpl remoteApi = new RemoteApiImpl();
							if(remoteApi.followSomeone(AppsOtherUserActivity.this, other_uid, false))
							{
								Message msg = new Message();
								msg.what = Constants.MSG_ACTION_UNFOLLOW_OK;
								mHandler.sendMessage(msg);
							}
							else
							{
								Message msg = new Message();
								msg.what = Constants.MSG_ACTION_UNFOLLOW_ERROR;
								mHandler.sendMessage(msg);
							}
						}
					};
					followThread.start();
				}
				catch (Exception e) {
					
					Toast.makeText(AppsOtherUserActivity.this, R.string.unfollow_error, 2000).show();
					e.printStackTrace();
				}
			}
		});

		Bundle bundle = getIntent().getExtras();
		if (bundle != null)
		{
			uid = bundle.getString("uid");
			other_uid = bundle.getString("other_uid");
			user_name = bundle.getString("user_name");
			if (user_name != null)
				label_user_name.setText(user_name);
			topbarTxt = (TextView) findViewById(R.id.topbar_txt);
			topbarTxt.setText(user_name + getString(R.string.word_DE_space));
			
			user_icon = bundle.getString("user_icon");
			Drawable iconDraw = null;
			try
			{
				if(user_icon.contains("buildin"))
					iconDraw = Util.getDrawableFromAsset(AppsOtherUserActivity.this, user_icon);
				else
					iconDraw = Util.getDrawableFromCache(AppsOtherUserActivity.this, user_icon);
				
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			if(iconDraw != null)
				img_user_icon.setImageDrawable(iconDraw);
			else
				img_user_icon.setImageResource(R.drawable.some_user);

			currentControl = UserAppPage.UserAppPage;
			startGetUserAppThread();
		}

	}

	class ApplicationGridViewListener implements OnItemClickListener
	{
		private List<OtherUserApp> mList;

		public ApplicationGridViewListener(List<OtherUserApp> list)
		{
			this.mList = list;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		{
			try
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(AppsOtherUserActivity.this, ApplicationActivity.class);
				OtherUserApp app = mList.get(position);

				Bundle mBundle = new Bundle();
				mBundle.putString("package_name", app.package_name);
				mBundle.putInt("n", 20);
				String uid = LocalStore.getLocaleStoreInstance(AppsOtherUserActivity.this).getUid();
				mBundle.putString("uid", uid);
				mBundle.putBoolean("installed_not_addicted", true);
				
				PackageManager manager = AppsOtherUserActivity.this.getPackageManager();
				try 
				{
					PackageInfo info = manager.getPackageInfo(app.package_name, 0);
					if (info != null)
						mBundle.putBoolean("user_app_flag", true);
					else
						mBundle.putBoolean("user_app_flag", false);
				
				} catch (Exception e) {
					mBundle.putBoolean("user_app_flag", false);
				}

				mBundle.putString("app_icon", app.icon_url);
				if (app.icon_drawable != null)
				{
					try
					{
						Bitmap bitmap = ((BitmapDrawable) app.icon_drawable).getBitmap();
						ByteArrayOutputStream stream = new ByteArrayOutputStream();

						bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
						byte[] bitmapdata = stream.toByteArray();
						mBundle.putByteArray("app_icon_array", bitmapdata);
					} catch (Exception ex)
					{
						ex.printStackTrace();
						mBundle.putByteArray("app_icon_array", null);
					}

				} else
				{
					mBundle.putByteArray("app_icon_array", null);
				}
				mBundle.putString("app_name", app.name);
				intent.putExtras(mBundle);
				startActivity(intent);

			} catch (Exception ex)
			{
				ex.printStackTrace();
			}

		}

	}

	@Override
	protected void onDestroy()
	{
		if(userAppAdapter != null)
		{
			userAppAdapter.freeDrawable();
			userAppAdapter = null;
		}
		if(feedAdapter != null)
		{
			feedAdapter.freeDrawable();
			feedAdapter = null;
		}
		if(userActivityAdapter != null)
		{
			userActivityAdapter.freeDrawable();
			userActivityAdapter = null;
		}
		super.onDestroy();
	}

	
	// 获得用户安装的应用程序列表
	private void startGetUserApp(){
		hideCurrentPage();
		topbarTxt.setText(user_name + getString(R.string.word_DE));
		userInfoLinear.setVisibility(View.VISIBLE);
		currentControl = UserAppPage.UserAppPage;
		progress_user_apps.setVisibility(View.VISIBLE);
		
		userAppThread = new OtherUserAppThread(this, mHandler, uid, other_uid);
		userAppThread.start();
	}
	
	// 获得用户的关注列表
	private void startGetUserFollowed()
	{
		hideCurrentPage();
		topbarTxt.setText(user_name + getString(R.string.word_defollowed));
		currentControl = UserAppPage.UserFlowPage;
		userInfoLinear.setVisibility(View.VISIBLE);
		progress_user_apps.setVisibility(View.VISIBLE);
		
		int n = 36;
		userFollowedThread = new FeedsThread(this, mHandler, n, other_uid, Constants.USER_TYPE_FOLLOWED, Constants.feedGetTypeAll);
		userFollowedThread.start();
	}
	
	// 获得用户的粉丝列表
	private void startGetUserFollower()
	{
		hideCurrentPage();
		topbarTxt.setText(user_name + getString(R.string.word_defollower));
		currentControl = UserAppPage.UserFansPage;
		userInfoLinear.setVisibility(View.VISIBLE);
		progress_user_apps.setVisibility(View.VISIBLE);
		
		int n = 36;
		userFollowerThread = new FeedsThread(this, mHandler, n, other_uid, Constants.USER_TYPE_FOLLOWER, Constants.feedGetTypeAll);
		userFollowerThread.start();
	}
	
	// 获得用户的分享动态列表
	private void startGetActivityThread() 
	{
		hideCurrentPage();
		topbarTxt.setText(user_name + getString(R.string.word_deaction));
		currentControl = UserAppPage.UserActionPage;
		progress_user_apps.setVisibility(View.VISIBLE);
		mActivityThread = new ActivityUserThread(this, mHandler, other_uid, 12);
		mActivityThread.start();
	}
	
	private void hideCurrentPage()
	{
		if(isEmptyPromptTV.getVisibility() == View.VISIBLE){
			isEmptyPromptTV.setVisibility(View.GONE) ;
		}
		switch(currentControl)
		{
			case UserAppPage:
				progress_user_apps.setVisibility(View.INVISIBLE);
				mAppGridView.setVisibility(View.INVISIBLE) ;
				userInfoLinear.setVisibility(View.GONE);
				break ;
				
			case UserFlowPage:
				progress_user_apps.setVisibility(View.INVISIBLE);
				mAppGridView.setVisibility(View.INVISIBLE) ;
				userInfoLinear.setVisibility(View.GONE);
				if (userFollowedThread != null && userFollowedThread.isAlive())
					userFollowedThread.stopRun();
				break ;
				
			case UserFansPage:
				progress_user_apps.setVisibility(View.INVISIBLE);
				mAppGridView.setVisibility(View.INVISIBLE) ;
				userInfoLinear.setVisibility(View.GONE);
				if (userFollowerThread != null && userFollowerThread.isAlive())
					userFollowerThread.stopRun();
				break ;
				
			case UserActionPage:
				progress_user_apps.setVisibility(View.INVISIBLE);
				userActivityListView.setVisibility(View.INVISIBLE);
				if (mActivityThread != null && mActivityThread.isAlive())
					mActivityThread.stopRun();
				break ;
		}
	}
	
	public void refreshGridView(final Feeds feeds)
	{
		if(feeds.feeds != null && feeds.feeds.size() == 0){
			if(currentControl == UserAppPage.UserFansPage){
				//无粉丝
				isEmptyPromptTV.setText(R.string.txt_other_no_fllower) ;
			} else if(currentControl == UserAppPage.UserFlowPage){
				//无关注
				isEmptyPromptTV.setText(R.string.txt_other_no_fllowed) ;
			}
			
			isEmptyPromptTV.setVisibility(View.VISIBLE) ;			
		}

		feedAdapter = new FeedsAdapter(this, feeds.feeds);

		mAppGridView.setVisibility(View.VISIBLE);
		mAppGridView.setNumColumns(4);
		mAppGridView.setAdapter(feedAdapter);
		mAppGridView.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView parent, View v, int position, long id)
			{
				try 
				{
					Feed userInfo = feeds.feeds.get(position);
					Intent intent = new Intent(); 
					intent.setClass(AppsOtherUserActivity.this, AppsOtherUserActivity.class);

					Bundle mBundle = new Bundle();
					String uid = LocalStore.getLocaleStoreInstance(AppsOtherUserActivity.this).getUid();
					mBundle.putString("uid", uid);
					mBundle.putString("other_uid", userInfo.uid);
					mBundle.putString("user_icon", userInfo.user_icon);
					mBundle.putString("user_name", userInfo.user_name);
					intent.putExtras(mBundle);
					startActivity(intent);
					
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		
	}
	
	public void refreshRecentActivity(List<UserActivity> list) 
	{
		mBottomButtonLayout.setVisibility(View.GONE);
		userActivityAdapter = new ActivityUserAdapter(this, list, user_name);
		userActivityListView.setVisibility(View.VISIBLE);
		userActivityListView.setAdapter(userActivityAdapter);
	}
	
	

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
			if(UserAppPage.UserActionPage==currentControl){
				mBottomButtonLayout.setVisibility(View.VISIBLE);
				startGetUserApp();
				return true;
			}
			else {
				return super.onKeyDown(keyCode, event) ;
			}
		} else {
			return super.onKeyDown(keyCode, event) ;
		}
		
	}	
	
}
